

//
//  NewsViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/21.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "NewsViewController.h"
#import "RootBarViewController.h"
#import "LogingViewController.h"

@interface NewsViewController () <UIScrollViewDelegate>

@property (nonatomic ,strong) UIPageControl *page;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.frame = self.view.bounds;
    scroll.contentSize = CGSizeMake(3 * scroll.frame.size.width, 0);
    [self.view addSubview:scroll];
    
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height);
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i + 1]];
        
        imageView.image = image;
        [scroll addSubview:imageView];
        if (i == 2) {
            [self setLastImageView:imageView];
        }
    }
    scroll.delegate = self;
    scroll.bounces = NO;
    scroll.pagingEnabled = YES;
    UIPageControl *page = [[UIPageControl alloc] init];
    page.numberOfPages = 3;
    
    page.currentPageIndicatorTintColor = [UIColor orangeColor];
    page.pageIndicatorTintColor = [UIColor grayColor];
    page.centerX = self.view.center.x;
    page.centerY = self.view.frame.size.height - 100;
    [self.view addSubview:page];
    self.page = page;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double page = scrollView.contentOffset.x / scrollView.width;
    self.page.currentPage = (int)(page + 0.5);
}

- (void)setLastImageView:(UIImageView *)imageView{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 1.分享给大家（checkbox）
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"3"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
//    shareBtn.backgroundColor = [UIColor redColor];
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    // 2.开始微博
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    
    startBtn.backgroundColor = [UIColor yellowColor];
    
//    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.width = 200;
    startBtn.height = 30;
    startBtn.centerX = shareBtn.centerX;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)shareClick:(UIButton *)shareBtn
{
    // 状态取反
    shareBtn.selected = !shareBtn.isSelected;
}

- (void)startClick
{
    // 切换到HWTabBarController
    /*
     切换控制器的手段
     1.push：依赖于UINavigationController，控制器的切换是可逆的，比如A切换到B，B又可以回到A
     2.modal：控制器的切换是可逆的，比如A切换到B，B又可以回到A
     3.切换window的rootViewController
     */
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[LogingViewController alloc] init];
    
    
    // modal方式，不建议采取：新特性控制器不会销毁
    //    HWTabBarViewController *main = [[HWTabBarViewController alloc] init];
    //    [self presentViewController:main animated:YES completion:nil];
}
@end
